package edu.sctu;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

/**
 * Created by 1 on 2018/5/26.
 */
public class UserAddServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);

        //获取用户输入信息
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        // 3+1  将用户信息添加到数据库
        Connection connection = null;
        Statement statement = null;

        // t_users
        String sql = "insert into t_users(username,password) values('"+username+"', '"+password+"')";

        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/sctu_javaweb_2018?serverTimezone=UTC",
                    "root",
                    "123456"
            );

            statement = connection.createStatement();
            statement.execute(sql);

            statement.close();
            connection.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        req.getRequestDispatcher("/userAdd.jsp")
                .forward(req,resp);
    }
}
